package com.budwk.app.enterprise.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.enterprise.models.ExternalInvestmentManagement;
import com.budwk.app.enterprise.services.ExternalinvestmentmanagementService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/enterprise/enterprise/external/investment")
@ApiDefinition(tag = "对外投资管理表")
@Slf4j
public class ExternalinvestmentmanagementController{
    @Inject
    private ExternalinvestmentmanagementService  wxExternalInvestmentService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询对外投资管理表")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = ExternalInvestmentManagement.class
    )
    @ApiResponses(
    implementation = ExternalInvestmentManagement.class
    )
    @SaCheckPermission("enterprise.enterprise.wx.external.investment")
    public Result<?> list(@Param("..")ExternalInvestmentManagement bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getInvestedEnterpriseName() !=null && Strings.isNotBlank(bean.getInvestedEnterpriseName().toString())) {
            cnd.and("investedEnterpriseName","=",bean.getInvestedEnterpriseName());
        }
        
        if (bean.getStatus() !=null && Strings.isNotBlank(bean.getStatus().toString())) {
            cnd.and("status","=",bean.getStatus());
        }
        
        if (bean.getEstablishmentDate() !=null && Strings.isNotBlank(bean.getEstablishmentDate().toString())) {
            cnd.and("establishmentDate","=",bean.getEstablishmentDate());
        }
        
        if (bean.getShareholdingRatio() !=null && Strings.isNotBlank(bean.getShareholdingRatio().toString())) {
            cnd.and("shareholdingRatio","=",bean.getShareholdingRatio());
        }
        
        if (bean.getAmountCapitalSubscribed() !=null && Strings.isNotBlank(bean.getAmountCapitalSubscribed().toString())) {
            cnd.and("amountCapitalSubscribed","=",bean.getAmountCapitalSubscribed());
        }
        
        if (bean.getFinalBeneficiaryShares() !=null && Strings.isNotBlank(bean.getFinalBeneficiaryShares().toString())) {
            cnd.and("finalBeneficiaryShares","=",bean.getFinalBeneficiaryShares());
        }
        
        if (bean.getRegion() !=null && Strings.isNotBlank(bean.getRegion().toString())) {
            cnd.and("region","=",bean.getRegion());
        }
        
        if (bean.getIndustry() !=null && Strings.isNotBlank(bean.getIndustry().toString())) {
            cnd.and("industry","=",bean.getIndustry());
        }
        
        if (bean.getRelatedProductsOrganizations() !=null && Strings.isNotBlank(bean.getRelatedProductsOrganizations().toString())) {
            cnd.and("relatedProductsOrganizations","=",bean.getRelatedProductsOrganizations());
        }
        
        return Result.data( wxExternalInvestmentService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增对外投资管理表")
    @ApiFormParams(
        implementation = ExternalInvestmentManagement.class
    )
    @ApiResponses
    @SLog(tag = "新增对外投资管理表", value = "${args[0].id}")
    @SaCheckPermission("enterprise.enterprise.wx.external.investment.create")
    public Result<?> create(@Param("..")ExternalInvestmentManagement  wxExternalInvestment, HttpServletRequest req) {
         wxExternalInvestment.setCreatedBy(SecurityUtil.getUserId());
         wxExternalInvestmentService.insert( wxExternalInvestment);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改对外投资管理表")
    @ApiFormParams(
        implementation = ExternalInvestmentManagement.class
    )
    @ApiResponses
    @SLog(tag = "修改对外投资管理表", value = "${args[0].id}")
    @SaCheckPermission("enterprise.enterprise.wx.external.investment.update")
    public Result<?> update(@Param("..")ExternalInvestmentManagement  wxExternalInvestment, HttpServletRequest req) {
         wxExternalInvestment.setUpdatedBy(SecurityUtil.getUserId());
         wxExternalInvestmentService.updateIgnoreNull( wxExternalInvestment);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取对外投资管理表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("enterprise.enterprise.wx.external.investment")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        ExternalInvestmentManagement  wxExternalInvestment =  wxExternalInvestmentService.fetch(id);
        if ( wxExternalInvestment == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data( wxExternalInvestment);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除对外投资管理表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除对外投资管理表", value = "${args[0]}")
    @SaCheckPermission("enterprise.enterprise.wx.external.investment.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        ExternalInvestmentManagement  wxExternalInvestment =  wxExternalInvestmentService.fetch(id);
        if ( wxExternalInvestment == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
         wxExternalInvestmentService.delete(id);
        req.setAttribute("Slog_msg",  wxExternalInvestment.getId());
        return Result.success();
    }
}
